package com.zd.commodity.brandMng.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;

import utils.ExcelUtil;



import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.brandMng.biz.IBrandShowBiz;
import com.zd.commodity.brandMng.entity.BrandShow;
import com.zd.commodity.categoryMng.biz.ICategoryBiz;
import com.zd.commodity.categoryMng.entity.Category;
import com.zd.commodity.userMng.entity.User;

public class BrandShowAction extends BaseAction<BrandShow> {
	private IBrandShowBiz brandShowBiz;

	public IBrandShowBiz getBrandShowBiz() {
		return brandShowBiz;
	}

	public void setBrandShowBiz(IBrandShowBiz brandShowBiz) {
		this.brandShowBiz = brandShowBiz;
		this.setBaseBiz(brandShowBiz);
	}
	
	public String toChooseCategoryMain(){
		request.setAttribute("params", super.getParams());
		return "toChooseCategoryMain";
	}
	public String toChooseCategoryTree(){
		request.setAttribute("params", super.getParams());
		return "toChooseCategoryTree";
	}

	@Autowired
	private ICategoryBiz categoryBiz;

	public String findBrandsByCategory(){
		//将参数转型
        String params=super.getParams();
        int page=super.getPage();
        int rows=super.getRows();
        BrandShow brandShow=super.getT1();
        
        Map<String, String> map = new HashMap<String, String>();
        String categoryId=request.getParameter("categoryId");
        //String categoryName=request.getParameter("categoryName");
        //ActionContext.getContext().put("categoryId", categoryId);
        //ActionContext.getContext().put("categoryName", categoryName);
        Category category=categoryBiz.get(Long.valueOf(categoryId));
        ActionContext.getContext().put("category", category);
        
        
		Object objparam=(Object)params;
		//页码换算
		if(page==0)
		{
			page=1;
		}
		if(rows==0)
		{
			rows=10;
		}	
		
		int firstResult=(page-1)*rows;
		
		if(brandShow!=null){
			map.put("brandName", brandShow.getName());
		}else{
			map.put("brandName", "");
		}
		map.put("categoryId", categoryId);
		
		
		//获取列表
		List<BrandShow> list=brandShowBiz.getBrandShowsByCategory(BrandShow.class, "getBrandShowsByCategoryId", map, firstResult, rows);
		//统计记录个数
		long count = brandShowBiz.getBrandShowsByCategoryCount("getBrandShowsByCategoryIdCount", map);
		long pageCount=count/rows;
		if(count%rows>0){
			pageCount++;
		}
		ActionContext.getContext().put("list", list);
		ActionContext.getContext().put("pageCount",pageCount);
		super.setPage(page);
		ActionContext.getContext().put("recordsCount",count);
		return "toshowBrands";
	}



	/**
	 * 下载模板
	 * @throws Exception 
	 */
	public void downloadTemplet() throws Exception{
		List brandTemplet = brandShowBiz.getExcelExportListBySql(BrandShow.class, "downloadTemplet", null, 0, 0);
		String[] title = new String[]{"品牌名称","品牌描述"};
		String[] neededFields = new String[]{"name","code"};
		super.excelExport("品牌上传模板", brandTemplet,title,neededFields);
	}
	
	/**
	 * 模板导入
	 * @throws Exception 
	 */
	public void uploadExeclData() throws Exception{
		String message  = "上传成功";
		HashMap<Integer, String> map = new HashMap<Integer ,String>();
		//map.put(0, "notNull");
		map.put(0, "queryRepeat|brand|name");
		List<String[]> data = brandShowBiz.readExecl(map);
		if(data.size()==1){
			String[] strings = data.get(0);
			if("0".equals(strings[0])){
				message = strings[1];
			}
		}
		String columRepeat = ExcelUtil.ExcelColumRepeat(data,new int[]{0});
		if(columRepeat!=null){
			message = columRepeat+"重复";
			
		}else{
			if(message.equals("上传成功")){
				for (String[] strings : data) {
					BrandShow brandShow = new BrandShow();
					brandShow.setName(strings[0]);
					brandShow.setDescription(strings[1]);
					brandShowBiz.add(brandShow);
				}
			}
		}
		
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		out.write(message);
		out.flush();
		out.close();
	}
	
	/**
	 * 数据导出
	 */
	public void excelExport() throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		BrandShow brandShow=super.getT1();
		map.put("name", brandShow.getName());
		
		List list = brandShowBiz.getExcelExportListBySql(BrandShow.class,"brandShowExcelExport",map.size()>0?map:null, -1, -1);
		
		String[] title = new String[]{"品牌名称","品牌描述","创建时间","创建人"};
		String[] neededFields = new String[]{"name","description","creationtime","creator"};
		super.excelExport("品牌管理", list,title,neededFields);
	}
	
	
	/**
	 * 判断品牌名称在数据库中是否已存在
	 */
	public void nameExist() throws IOException{
		String name=super.getT().getName();
		HashMap<String, String> map=new HashMap<String, String>();
		map.put("tableName","brand" );
		map.put("columName","name" );
		map.put("value", name);
		Long  size = brandShowBiz.getPagedListCountBySql("queryRepeat", map);
		JSONObject json=new JSONObject();
		
		if(size>0){
			json.put("valid", false);
		}else{
			json.put("valid", true);
		}
		response.getWriter().write(json.toString());
	}
}
